<template>
  <div class="z-question-container z-container" :id="'question_'+prefix">
    <!--                                      左侧序号等内容-->
    <!--    <div class="z-prefix-container">-->
    <!--      {{ prefix }}.-->
    <!--    </div>-->
    <div class="z-main-container">
      <!--                                   题干-->
      <div class="z-content-container z-long-text z-large-text">
        <p><strong>{{ prefix }}.【{{ options.QUESTION_TYPE.map[data.type] }}】</strong>{{ data.content }}</p>
      </div>
      <!--                                   答题内容-->
      <div class="z-container z-offset">
        <!--    判断题-->

        <el-radio-group v-if="options.QUESTION_TYPE.enums.JudgeQuestion.value===data.type" v-model="key"
                        :disabled="!isAnswering"
        >
          <el-radio v-for="item in options.JUDGE_QUESTION_RIGHT_KEY.list" :label="item.label">
            {{ item.value }}
          </el-radio>
        </el-radio-group>
        <!--    选择题-多选-->
        <el-checkbox-group v-model="key" :disabled="!isAnswering"
                           v-if="options.QUESTION_TYPE.enums.ChoiceQuestion.value===data.type && isMultipleChoice">
          <div v-for="(item, index) in data.examQuestionBankOptionList">
            <el-checkbox :label="index.toString()">
              {{ options.Choice_Question_Option.map[index] }}.{{ item.content }}
            </el-checkbox>
          </div>
        </el-checkbox-group>

        <!--    选择题-单选-->
        <el-radio-group v-model="key" :disabled="!isAnswering"
                        v-if="options.QUESTION_TYPE.enums.ChoiceQuestion.value===data.type && !isMultipleChoice">
          <el-radio v-for="(item, index) in data.examQuestionBankOptionList" :label="index.toString()">
            {{ options.Choice_Question_Option.map[index] }}.{{ item.content }}
          </el-radio>
        </el-radio-group>



        <!--    填空题-->
        <div v-if="options.QUESTION_TYPE.enums.FillBlankQuestionExcel.value===data.type">
          <el-input v-for="(item, index) in data.examQuestionBankOptionList" type="text" v-model="key[index]"
                    show-word-limit
                    :maxlength="255" style="margin-top: 10px" :disabled="!isAnswering">
            <template v-slot:prepend>
              <span>{{ index + 1 }}.</span>
            </template>
          </el-input>
        </div>
        <!--      论述题-->

        <el-input v-if="options.QUESTION_TYPE.enums.EssayQuestion.value===data.type" type="textarea"
                  :autosize="{ minRows: 2, maxRows: 12}" v-model="key" show-word-limit :maxlength="255"
                  :disabled="!isAnswering"></el-input>
      </div>
      <!--                                        得分以及正确错误-->
      <div class="z-container z-offset" v-if="!isAnswering">
        <span>实际得分：</span>
        <el-input-number :disabled="!isGRADE" :controls="false" v-model="gainGrade" :precision="1"
                         @blur="handleRemarkChange"/>
        <span style="margin-left: 20px">答卷结果：</span>
        <el-switch v-model="result" active-color="#13ce66"
                   inactive-color="#ff4949"
                   :active-value="options.RESULT_TYPE.enums.RIGHT.value"
                   :inactive-value="options.RESULT_TYPE.enums.ERROR.value"
                   :active-text="options.RESULT_TYPE.enums.RIGHT.label"
                   :inactive-text="options.RESULT_TYPE.enums.ERROR.label"
                   :disabled="!isGRADE"
                   @change="handleRemarkChange"
        />
      </div>

      <!--                                        答案-->
      <div class="z-container z-offset" v-if="!isAnswering">
        <div style="display: inline-block;width: 50% ;vertical-align: top">
          <span style="display: block">您的答案：</span>
          <p
            :class="[data.result === options.RESULT_TYPE.enums.RIGHT.value ? 'z-right-key':'z-wrong-key', 'z-long-text']">
            {{ resultContentStr }}
          </p>
        </div>
        <div style="display: inline-block;width: 50%;vertical-align: top">
          <span style="display: block">参考答案：</span>
          <!--          判断题-->
          <p class="z-right-key z-long-text">{{
              rightKeyStr
            }}</p>
          <!--          <span class="z-right-key"-->
          <!--                v-if="data.type===options.QUESTION_TYPE.enums.JudgeQuestion.value">{{-->
          <!--              options.JUDGE_QUESTION_RIGHT_KEY.map[data.rightKey]-->
          <!--            }}</span>-->
          <!--          &lt;!&ndash;          填空题&ndash;&gt;-->
          <!--          <span class="z-right-key" v-if="data.type===options.QUESTION_TYPE.enums.FillBlankQuestionExcel.value"-->
          <!--                v-for="item in data.examQuestionBankOptionList">{{ item.content }}</span>-->
          <!--          &lt;!&ndash;          论述题&ndash;&gt;-->
          <!--          <p class="z-right-key z-long-text"-->
          <!--             v-if="data.type===options.QUESTION_TYPE.enums.EssayQuestion.value">{{ data.rightKey }}</p>-->
          <!--          &lt;!&ndash;          选择题&ndash;&gt;-->
          <!--          <span class="z-right-key"-->
          <!--                v-if="data.type===options.QUESTION_TYPE.enums.ChoiceQuestion.value">{{-->
          <!--              options.Choice_Question_Option.map[data.rightKey]-->
          <!--            }}</span>-->
        </div>
      </div>
      <!--                                        解析-->
      <div class="z-analysis-container z-container z-offset" v-if="!isAnswering">
        <i class='el-icon-s-order z-long-text'
           style="flex-basis: 60px;color: rgb(96, 98, 102);flex-shrink: 0;flex-grow: 0;  flex-wrap: wrap;">解析:</i>
        <span class="z-long-text">{{ data.analysis }}</span>
      </div>
      <!--                                        批注-->
      <div class="z-analysis-container z-container z-offset" v-if="!isAnswering">
        <i class='el-icon-edit z-long-text'
           style="flex-basis: 60px;color: rgb(96, 98, 102);flex-shrink: 0;flex-grow: 0;  flex-wrap: wrap;">批注:</i>
        <el-input type="textarea"
                  :autosize="{ minRows: 2, maxRows: 12}" v-model="remark" show-word-limit :maxlength="255"
                  :disabled="!isGRADE"
                  @blur="handleRemarkChange"/>
      </div>
    </div>

    <span class="z-grade">({{ data.grade }}分)</span>
  </div>
</template>

<script>
import {
  Choice_Question_Option,
  EXAM_TEST_PAPER_STATE,
  JUDGE_QUESTION_RIGHT_KEY,
  QUESTION_TYPE,
  RESULT_TYPE
} from '@/views/train/Enums.js'
import {isString, isValid} from "@/utils/validate";
import {SEPARATOR} from '@/utils/Constant'
import mixins from "@/views/train/mixins";

export default {
  name: "question",
  mixins: [mixins],
  props: {
    prefix: {
      required: false,
      type: [String, Number],
      default: ''
    },
    // 试题内容,格式如下
    data: {
      required: true,
      validate(value) {
        return Object.prototype.toString.call(value) === '[object Object]'
      }
    },
    state: {
      required: true,
      type: [String, Number],
    }
  },
  watch: {
    key: {
      handler(value) {
        let data = {
          questionId: this.data.questionId,
          index: this.prefix,
        }
        if (this.data.type === this.options.QUESTION_TYPE.enums.FillBlankQuestionExcel.value) {
          let flag = true
          value.forEach(e => {
            if (e === '') {
              flag = false
            }
          })
          if (flag) {
            this.$emit('questionChange', {
              key: value.join(SEPARATOR),
              finished: true,
              ...data

            })
          } else {
            this.$emit('questionChange', {
              key: value.join(SEPARATOR),
              finished: false,
              questionId: this.data.questionId,
              ...data
            })
          }
        } else {
          if (isValid(value) && value !== '') {
            this.$emit('questionChange', {
              key: value,
              finished: true,
              ...data

            })
          } else {
            this.$emit('questionChange', {
              key: value,
              finished: false,
              ...data
            })
          }
        }
      },
      deep: true
    }
  },
  data() {
    let data = {
      // 这是用于回显填写答案和正确答案的
      resultContentStr: '',
      rightKeyStr: '',
      // 这是用于判卷的
      remark: '',
      gainGrade: 0,
      result: '',
      // 是否是答题状态
      isAnswering: Boolean(~[
        // EXAM_TEST_PAPER_STATE.enums.WAIT_RECEIVE.value,
        EXAM_TEST_PAPER_STATE.enums.WAIT_SUBMIT.value,
        // EXAM_TEST_PAPER_STATE.enums.WAIT_GRADE.value,
        // EXAM_TEST_PAPER_STATE.enums.FINISH.value,
      ].indexOf(this.state)),
      // 是否判卷状态
      isGRADE: Boolean(~[
        // EXAM_TEST_PAPER_STATE.enums.WAIT_RECEIVE.value,
        // EXAM_TEST_PAPER_STATE.enums.WAIT_SUBMIT.value,
        EXAM_TEST_PAPER_STATE.enums.WAIT_GRADE.value,
        // EXAM_TEST_PAPER_STATE.enums.FINISH.value,
      ].indexOf(this.state)),
      options: {
        EXAM_TEST_PAPER_STATE: EXAM_TEST_PAPER_STATE,
        QUESTION_TYPE: QUESTION_TYPE,
        JUDGE_QUESTION_RIGHT_KEY: JUDGE_QUESTION_RIGHT_KEY,
        Choice_Question_Option: Choice_Question_Option,
        RESULT_TYPE: RESULT_TYPE
      },
      // 单选/多选题
      isMultipleChoice: false,
    }
    if (this.data.type === QUESTION_TYPE.enums.FillBlankQuestionExcel.value) {
      return {
        ...data,
        // 答案
        key: [],
      }
    } else {
      return {
        ...data,
        key: undefined,
      }
    }

  },
  methods: {
    // 数据变动时的事件,用于批卷...
    handleRemarkChange(value) {
      if (this.isGRADE) {
        this.$emit('remarkChange', {
          questionId: this.data.questionId,
          remark: this.remark,
          gainGrade: this.gainGrade,
          result: this.result
        })
      }
    },
  },
  created() {
    this.result = isValid(this.data.result) ? this.data.result : this.options.RESULT_TYPE.enums.ERROR.value
    this.gainGrade = this.data.gainGrade || 0

    // 针对于正确答案以及用户填写答案的处理
    switch (this.data.type) {
      // 选择题
      case this.options.QUESTION_TYPE.enums.ChoiceQuestion.value:
        let resultKeys = this.data.rightKey.split(',')
        this.rightKeyStr = this.data.rightKey.split(',').map(e => this.options.Choice_Question_Option.map[e]).sort().toString()
        if (resultKeys.length > 1) {
          this.isMultipleChoice = true
        }
        // 判断是否是多选题
        if (this.isMultipleChoice) {
          // 多选题
          if (this.data.resultContent) {
            this.key = this.data.resultContent.split(',')
          } else {
            this.key = []
          }
          this.resultContentStr = this.key.map(e => this.options.Choice_Question_Option.map[e]).sort().toString()
          this.rightKeyStr = resultKeys.map(key => this.options.Choice_Question_Option.map[key]).sort().toString()
        } else {
          // 单选题
          if (this.data.resultContent) {
            this.key = this.data.resultContent
          } else {
            this.key = ''
          }
          this.resultContentStr = this.options.Choice_Question_Option.map[this.key]
          this.rightKeyStr = this.options.Choice_Question_Option.map[this.data.rightKey]
        }
        break
      // 论述题
      case this.options.QUESTION_TYPE.enums.EssayQuestion.value:
        this.key = this.data.resultContent
        this.resultContentStr = this.data.resultContent
        this.rightKeyStr = this.data.rightKey
        break

      // 填空题
      case this.options.QUESTION_TYPE.enums.FillBlankQuestionExcel.value:
        // 如果是填空题,那么需要手动将答案给分离出来
        let key = new Array(this.data.examQuestionBankOptionList.length).fill('')
        if (isString(this.data.resultContent)) {
          this.data.resultContent.split(SEPARATOR).forEach((result, index) => {
            key[index] = result
          })
        }
        this.key = key
        this.resultContentStr = this.data.resultContent.split(SEPARATOR).join('\t')
        this.rightKeyStr = this.data.examQuestionBankOptionList.map(e => e.content).join('\t')
        break

      // 判断题
      case this.options.QUESTION_TYPE.enums.JudgeQuestion.value:
        this.key = ~Object.keys(this.options.JUDGE_QUESTION_RIGHT_KEY.map).indexOf(this.data.resultContent) ? this.data.resultContent : null
        this.resultContentStr = this.options.JUDGE_QUESTION_RIGHT_KEY.map[this.data.resultContent]
        this.rightKeyStr = this.options.JUDGE_QUESTION_RIGHT_KEY.map[this.data.rightKey]
        break
    }
    if (this.isGRADE) {
      this.$nextTick(() => {
        this.$emit('remarkChange', {
          questionId: this.data.questionId,
          remark: this.remark,
          gainGrade: this.gainGrade,
          result: this.result
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.z-question-container {
  overflow: auto;
  display: flex;
  flex-direction: row;
  font-family: Microsoft YaHei;
  speak: none;
  font-style: normal;
  font-weight: normal;
  font-variant: normal;
  text-transform: none;
  line-height: 1;
  vertical-align: baseline;
  -webkit-font-smoothing: antialiased;
  word-break: break-all;

  p {
    text-indent: 4ch;
    margin: 0;
  }

  .z-statistics-container {
    flex-grow: 0;
    flex-shrink: 0;
    flex-basis: 300px;
    flex-wrap: wrap;

  }

  .z-prefix-container {
    flex-basis: 50px;
    padding-right: 0;
    flex-shrink: 0;
    flex-grow: 0;
    flex-wrap: wrap;
  }

  .z-main-container {
    flex-grow: 1;
    flex-shrink: 1;
    padding-right: 20px;
    overflow: hidden;
  }


  .z-right-key {
    color: #13ce66
  }

  .z-wrong-key {
    color: #FF0000;
  }

  .z-analysis-container {
    /*border: 2px solid rgb(96, 98, 102);*/
    min-height: 50px;
    display: flex;
    flex-direction: row;
  }

  .z-container {
    padding-top: 15px;
  }

  .z-content-container {
    display: inline-block;
    /*border: 1px solid #dcdfe6;*/
    /*background-color: #eeeef0;*/
    box-sizing: border-box;

  }

  .z-content {
    display: inline-block;
    padding-right: 40px;
    flex-grow: 1;
    white-space: pre-line;
    border: 1px solid black;
    box-sizing: border-box;
  }

  .z-grade {
    flex-grow: 0;
    flex-shrink: 0;
    flex-wrap: wrap;
    flex-basis: 60px;
    display: inline-block;
  }

  .el-radio {
    display: block;

    ::v-deep .el-radio__label {
      font-size: 16px !important;
    }

    ::v-deep .el-radio__inner {
      height: 16px !important;
      width: 16px !important;
    }
  }

  .el-radio + .el-radio {
    margin-top: 10px;
  }

  .z-long-text {
    line-height: 35px;
  }

  .z-large-text {
    font-size: 18px;
  }

  .z-offset {
    margin-left: 80px;
    margin-right: 40px;
  }
}


</style>
